package de.tabuma.lib.webservice.response;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.util.logging.*;

/**
 * Handler for integrating soap messages.
 * @author $Author: $
 * @version $Revision: $
 * @since $Date: $
 */
public class XmlHandler implements org.xml.sax.ContentHandler {

    private static final Logger log = Logger.getLogger(XmlHandler.class.getName());

    String url;
    Vector<Object> objectList;
    
    String name;
    Method method;
    List<Class> methodParameter;
    public List<Class> getMethodParameter() {
        return methodParameter;
    }

    List<String> valueParameter;
    public List<String> getValueParameter() {
        return valueParameter;
    }

    String targetNamespace;
    String prefix;
    boolean start;
    boolean readElement;
    
    public XmlHandler(InputStream in, String name, Method method, String targetNamespace) {
        try {
            this.name = name;
            this.method = method;
            this.methodParameter = new ArrayList();
            this.targetNamespace = targetNamespace;
            for(int i=0;i<method.getParameterTypes().length;i++) {
                methodParameter.add(method.getParameterTypes()[i]);
            }
            this.start = false;
            this.valueParameter = new ArrayList<String>();
            XMLReader parser = XMLReaderFactory.createXMLReader();
            parser.setContentHandler(this);
            InputSource inSource = new InputSource(in);
            parser.parse(inSource);
        }
        catch(Exception e) {
            log.warning(e.getMessage());
            String warning = "";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i].toString()+"\n";
            }
            log.warning(warning);
        }
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        if(this.readElement) {
            String value = "";
            for(int i=start;i<length+start;i++) {
                value += ch[i];
            }
            try {
                value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
            }
            catch(Exception e) {
                String error = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
                for(int i=0;i<e.getStackTrace().length;i++) {
                    error += e.getStackTrace()[i]+"\n";
                }
                log.severe(error);
            }
            valueParameter.add(value);
            this.readElement = false;
        }
    }

    public void startDocument() throws SAXException {
        log.finest("Start parsing");
    }

    public void endDocument() throws SAXException {
        log.finest("End parsing");
    }

    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        this.readElement = false;
        if(this.start==true) {
            this.readElement = true;
        }
        if(localName.equals("Request_"+name+"_"+method.getName())) {
            this.start = true;
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(localName.equals(name+"_"+method.getName())) {
            start = false;
            readElement = false;
        }
    }

    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        //
    }

    public void processingInstruction(String target, String data) throws SAXException {
        //
    }

    public void setDocumentLocator(Locator locator) {
        //
    }

    public void skippedEntity(String name) throws SAXException {
        //
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        log.finest("start prefix mapping:"+prefix+"/"+uri);
        if(uri.equals(targetNamespace)) {
            log.finest("setting prefix to "+prefix);
            this.prefix = prefix;
        }
    }

    public void endPrefixMapping(String prefix) throws SAXException {
        log.finest("end prefix mapping:"+prefix);
    }
}